//
//  CollocationController.m
//  cutter
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 e360. All rights reserved.
//

#import "PlansCenterCtrl.h"

#pragma mark -
#pragma mark PlansCenterCell
@implementation PlansCenterCell
- (void)awakeFromNib{
    [super awakeFromNib];
    if (self.infoView) {
        [self.infoView setHidden:YES];
    }
    if (self.selectView) {
        [self.selectView setHidden:YES];
    }
}
- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    [self.selectView setSelected:selected];
}
@end


#pragma mark -
#pragma mark PlansCenterList
@implementation PlansCenterList
-(void)viewDidLoad{
    [super viewDidLoad];
    //
    [self.deleteView setHidden:YES];
    [self.deleteView addTarget:self action:@selector(deleteTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.editView addTarget:self action:@selector(editTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionView setAllowsMultipleSelection:YES];
    //
    [self.collectionView.headerView addTarget:self action:@selector(headerRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.collectionView.headerView setTitle:@"正在刷新" forState:TMRefreshStateRefreshing];
    [self.collectionView.headerView setTitle:@"释放更新" forState:TMRefreshStatePulling];
    [self.collectionView.headerView setTitle:@"下拉刷新" forState:TMRefreshStateNormal];
    //
    [self.collectionView.footerView addTarget:self action:@selector(footerRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.collectionView.footerView setTitle:@"正在刷新" forState:TMRefreshStateRefreshing];
    [self.collectionView.footerView setTitle:@"释放更新" forState:TMRefreshStatePulling];
    [self.collectionView.footerView setTitle:@"上拉翻页" forState:TMRefreshStateNormal];
    //
    [self.searchView setDelegate:self];
    [self headerRefresh:nil];
}
//
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.condition setValue:textField.text forKey:@"keyword"];
    [self.condition setValue:@(1) forKey:@"page"];
    [self headerRefresh:nil];
    return YES;
}
//
- (void)headerRefresh:(TMRefresh*)sender{
    if (sender == nil) {
        sender = self.collectionView.headerView;
        [sender beginRefreshing];
    }
    //
    __weak PlansCenterList *this = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [this.condition setValue:@(1) forKey:@"page"];
        //
        [this setData:[AppData userPlan:this.condition]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [sender endRefreshing];
            [this.collectionView.footerView setFinish:NO];
            //
            [this.collectionView reloadData];
        });
    });
}
- (void)footerRefresh:(TMRefresh*)sender{
    __weak PlansCenterList *this = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSInteger p = [[self.condition objectForKey:@"page"] intValue] + 1;
        [this.condition setValue:@(p) forKey:@"page"];
        //
        NSArray *app = [AppData userPlan:this.condition];
        [this.data addObjectsFromArray:app];
        dispatch_async(dispatch_get_main_queue(), ^{
            [sender setFinish:[app count]==0];
            [sender endRefreshing];
            //
            [this.collectionView reloadData];
        });
    });
}
- (NSMutableDictionary *)condition{
    if (_condition == nil) {
        [self setCondition:[NSMutableDictionary dictionary]];
    }
    [_condition setValue:@(1) forKey:@"isshare"];
    [_condition setValue:@(50) forKey:@"limit"];
    return _condition;
}
//
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.data count] + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
        PlansCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"addIdentifier" forIndexPath:indexPath];
        return cell;
    }
    NSDictionary *item = [self.data objectAtIndex:indexPath.item - 1];
    PlansCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    [cell.titieView setText:[item objectForKey:@"name"]];
    [cell.priceView setText:[item objectForKey:@"price"]];
    [AppData loadImage:[item objectForKey:@"image"] view:cell.imageView];
    return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    __weak PlansCenterList *this = self;
    if ([segue.destinationViewController isKindOfClass:PlansCenterCtrl.class]) {
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
        if (indexPath) {
            PlansCenterCtrl *ctrl = segue.destinationViewController;
            if (indexPath.item > 0) {
                [ctrl import:[self.data objectAtIndex:indexPath.item - 1]];
            }
            [ctrl setOnSelected:^(id target, id result){
                [this headerRefresh:nil];
                [this dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }
}
//
- (void)editTouch:(UIButton*)sender{
    [sender setSelected:!sender.selected];
    [self.deleteView setHidden:!sender.selected];
    //
    NSArray *cells = [self.collectionView visibleCells];
    for (PlansCenterCell *cell in cells) {
        if ([cell isKindOfClass:PlansCenterCell.class]) {
            [cell.selectView setHidden:!sender.selected];
        }
    }
}
- (void)deleteTouch:(UIButton*)sender{
    NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    if ([indexPaths count] > 0) {
        for (NSIndexPath *indexPath in indexPaths.reverseObjectEnumerator) {
            id item = [self.data objectAtIndex:indexPath.item];
            [self.data removeObject:item];
        }
        [self.collectionView deleteItemsAtIndexPaths:indexPaths];
    }
    [self editTouch:self.editView];
}
@end


//
@implementation PlansCenterCtrl
-(void)import:(NSDictionary*)data{
    
}
- (IBAction)exitTouch:(UIButton*)sender {
    if (self.onSelected) {
        self.onSelected(self, nil);
    }else{
        IndexHomeCtrl *tabbar = (id)self.splitViewController;
        if ([tabbar isKindOfClass:IndexHomeCtrl.class]) {
            [tabbar showMenu];
        }
    }
}
@end
